using System;
using WCell.RealmServer.Chat;
using WCell.RealmServer.Entities;
using WCell.RealmServer.Global;
using WCell.RealmServer.Misc;
using WCell.Util;
using WCell.Util.Commands;

namespace WCell.RealmServer.Commands
{

	#region Ingame
	/// <summary>
	/// Represents a trigger for commands through ingame chat
	/// </summary>
	public class IngameCmdTrigger : RealmServerCmdTrigger
	{
		public IngameCmdTrigger(StringStream text, IUser user, IGenericChatTarget target, bool dbl)
			: base(text, new RealmServerCmdArgs(user, dbl, target))
		{
		}

		public IngameCmdTrigger(RealmServerCmdArgs args)
			: base(args)
		{
		}

		public override void Reply(string txt)
		{
			Args.Character.SendSystemMessage(txt);
		}

		public override void ReplyFormat(string txt)
		{
			Args.Character.SendSystemMessage(txt);
		}
	}
	#endregion

	#region Console
	/// <summary>
	/// Default trigger for console
	/// </summary>
	public class DefaultCmdTrigger : RealmServerCmdTrigger
	{

		public DefaultCmdTrigger()
		{
		}

		public DefaultCmdTrigger(string text,BaseCommand<RealmServerCmdArgs> selectedCommand, RealmServerCmdArgs args)
			: base(new StringStream(text), selectedCommand, args)
		{
		}

		public DefaultCmdTrigger(string text)
			: base(new StringStream(text), null, new RealmServerCmdArgs(null, false, null))
		{
		}

		public DefaultCmdTrigger(string text, BaseCommand<RealmServerCmdArgs> selectedCommand)
			: base(new StringStream(text), selectedCommand, new RealmServerCmdArgs(null, false, null))
		{
		}

		public DefaultCmdTrigger(StringStream args)
			: base(args, null, new RealmServerCmdArgs(null, false, null))
		{
		}

		public DefaultCmdTrigger(StringStream args, BaseCommand<RealmServerCmdArgs> selectedCommand)
			: base(args, selectedCommand, new RealmServerCmdArgs(null, false, null))
		{
		}

		public override void Reply(string txt)
		{
			Console.ForegroundColor = ConsoleColor.White;
			Console.WriteLine(txt);
			Console.ResetColor();
		}

		public override void ReplyFormat(string txt)
		{
			Console.ForegroundColor = ConsoleColor.White;
			Console.WriteLine(ChatUtility.Strip(txt));
			Console.ResetColor();
		}
	}
	#endregion

	public abstract class RealmServerCmdTrigger : CmdTrigger<RealmServerCmdArgs>
	{
		protected RealmServerCmdTrigger()
		{
		}

		protected RealmServerCmdTrigger(StringStream text, RealmServerCmdArgs args) :
			base(text, args)
		{
		}

		protected RealmServerCmdTrigger(StringStream text, BaseCommand<RealmServerCmdArgs> selectedCmd, RealmServerCmdArgs args) :
			base(text, selectedCmd, args)
		{
		}

		protected RealmServerCmdTrigger(RealmServerCmdArgs args) :
			base(args)
		{
		}

		/// <summary>
		/// Check for selected Target and selected Command
		/// </summary>
		public bool InitTrigger()
		{
			if (Args.Role.IsStaff)
			{
				var mod = m_text.NextModifiers();
				if (mod.Length > 0)
				{
					var chr = GetCharacter(mod);
					if (chr != null)
					{
						Args.Character = chr;
						Args.Double = false;
					}
					else
					{
						return false;
					}
				}
				else if (Args.Character != null && selectedCmd == null)
				{
					selectedCmd = Args.Character.ExtraInfo.SelectedCommand;
				}
			}
			return true;
		}

		/// <summary>
		/// Sets the Character of this trigger, according to the -a or -c switch, followed by the account- or character-name
		/// </summary>
		/// <param name="mod"></param>
		/// <returns></returns>
		public Character GetCharacter(string mod)
		{
			Character chr = null;
			var role = Args.Role;
			var isAcc = mod.Contains("a");
			var isChar = mod.Contains("c");
			if (isAcc || isChar)
			{
				if (isAcc && isChar)
				{
					Reply("Invalid command-switch, cannot use -a and -c switch at the same time.");
				}
				else
				{
					if (role != null && !role.CanUseCommandsOnOthers)
					{
						Reply("You may not use the -c or -a command-switch!");
					}

					var name = m_text.NextWord();
					if (isAcc)
					{
						var acc = RealmServer.Instance.GetLoggedInAccount(name);
						if (acc == null || (chr = acc.ActiveCharacter) == null)
						{
							Reply("Account {0} is not online.", name);
						}
					}
					else
					{
						chr = World.GetCharacter(name, false);
						if (chr == null)
						{
							Reply("Character {0} is not online.", name);
						}
					}

					if (chr != null)
					{
						if (role != null && chr.Account.Role > role)
						{
							if (isAcc)
							{
								// disguise accounts - don't let anyone guess account-names of higher staff members
								Reply("Account {0} is not online.", name);
							}
							else
							{
								if (chr.Stealthed == 0)
								{
									Reply("Cannot use this Command on {0}.", chr.Name);
								}
								else
								{
									Reply("Character {0} is not online.", name);
								}
							}
						}
						else
						{
							return chr;
						}
					}
				}
			}
			else
			{
				Reply("Invalid Command-Switch: " + mod);
			}
			return null;
		}
	}
}